package crm.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import crm.entity.Product;
import crm.service.ProductService;
import crm.service.impl.ProductServiceImpl;

/**
 * Servlet implementation class SearchServlet
 */
public class ProducttSearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ProductService productService = new ProductServiceImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	      doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String search = request.getParameter("search");
		System.out.println(search);
		
		try {
			
			
			List<Product> productLike = productService.getProductLike("%"+search+"%");
			
			if(productLike == null) {
				int E = 1;
				request.setAttribute("E", E);
				request.getRequestDispatcher("/product_seek.jsp").forward(request, response);
			}else {
				
				System.out.println(productLike);
				request.setAttribute("productLike",productLike);
				request.getRequestDispatcher("/product_seek.jsp").forward(request, response);
			}
			
			
			
			
		} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}
